% Feature Selection By Forward Selection
clc;
clearvars;
FSDataFileName='Data\FrwdSelectData_ByMAE_All_68NN.mat';
BEDataFileName='Data\BcwrdEliminData_ByMAE_All_68NN.mat';
FSData=load(FSDataFileName);
BEData=load(BEDataFileName);

MaxFNum=FSData.MaxFNum;
FNum=FSData.FNum;
%% Extract Datas
FSFeaturesIndex=FSData.Features{end};

BEFeaturesIndex=zeros(1,BEData.FNum);
BEFeaturesIndex(1)=setdiff(1:BEData.FNum,BEData.Features{1});
for i_Step=2:BEData.FNum
    BEFeaturesIndex(i_Step)=setdiff(BEData.Features{i_Step-1},BEData.Features{i_Step});
end
%% Revers Backward Elimination
BEFeaturesIndex=BEFeaturesIndex(end:-1:1);
BEData.BestMAE=BEData.BestMAE(end:-1:1);
%% Display Best Features Index
[~,FSSelectNum]=min(FSData.BestMAE);
[~,BESelectNum]=min(BEData.BestMAE);

fprintf('Selected Features By FS = [%02d',FSFeaturesIndex(1))
fprintf(',%02d',FSFeaturesIndex(2:FSSelectNum))
fprintf(']\n << %d Features>>\n',FSSelectNum)

fprintf('Selected Features By BE = [%02d',BEFeaturesIndex(1))
fprintf(',%02d',BEFeaturesIndex(2:BESelectNum))
fprintf(']\n << %d Features>>\n',BESelectNum)

%%
fprintf('Imporovment By FS = %f\n',FSData.MAEALL-FSData.BestMAE(FSSelectNum))
fprintf('Imporovment BY BE = %f\n',BEData.MAEALL-BEData.BestMAE(BESelectNum))
fprintf('Imporovment Percentage By FS = %f%%\n',(FSData.MAEALL-FSData.BestMAE(FSSelectNum))/FSData.MAEALL*100)
fprintf('Imporovment Percentage BY BE = %f%%\n',(BEData.MAEALL-BEData.BestMAE(BESelectNum))/BEData.MAEALL*100)
fprintf('FS) Min Imporove = %f ,Last Improve = %f  ,Next Improve = %f\n',-max(diff(FSData.BestMAE(1:FSSelectNum))),FSData.BestMAE(FSSelectNum-1)-FSData.BestMAE(FSSelectNum),FSData.BestMAE(FSSelectNum)-FSData.BestMAE(FSSelectNum+1))
fprintf('BE) Min Imporove = %f ,Last Improve = %f  ,Next Improve = %f\n',-max(diff(BEData.BestMAE(1:BESelectNum))),BEData.BestMAE(BESelectNum-1)-BEData.BestMAE(BESelectNum),BEData.BestMAE(BESelectNum)-BEData.BestMAE(BESelectNum+1))
fprintf('Last Imporovment Percentage By FS = %f%% -> %f%%\n',(FSData.BestMAE(FSSelectNum-1)-FSData.BestMAE(FSSelectNum))/FSData.BestMAE(FSSelectNum)*100,(FSData.BestMAE(FSSelectNum+1)-FSData.BestMAE(FSSelectNum))/FSData.BestMAE(FSSelectNum)*100)
fprintf('Last Imporovment Percentage By BE = %f%% -> %f%%\n',(BEData.BestMAE(BESelectNum-1)-BEData.BestMAE(BESelectNum))/BEData.BestMAE(BESelectNum)*100,(BEData.BestMAE(BESelectNum+1)-BEData.BestMAE(BESelectNum))/BEData.BestMAE(BESelectNum)*100)
%% Plot OPtion
BasePlotFileName='Plots\FetureSelComp_Fig';
TextSize=14;
InFigsTextSize=12;
SavePlots=0;
%% Plot
figure(1);
subplot(2,1,1)
FSSortedFeaturesName=FSData.FeaturesName(FSFeaturesIndex);
stairs(1:MaxFNum+2 ,[FSData.BestMAE(1:MaxFNum),FSData.MAEALL,FSData.MAEALL],'linewidth',2)
hold on
stairs(1:FSSelectNum+1,FSData.BestMAE(1:FSSelectNum+1),'r','linewidth',2)
hold off
for i=1:MaxFNum
    text(i+0.5,FSData.BestMAE(i)+0.002,FSSortedFeaturesName{i},'Rotation',90,'FontSize',InFigsTextSize)
end
text(MaxFNum+1.5,FSData.MAEALL+0.005,'All Features','Rotation',90,'FontSize',InFigsTextSize)
text(-2.5,0.187,'A)','FontSize',TextSize,'FontWeight','Bold')
ylim([0.182,0.195])
xlim([0.5,MaxFNum+2.5])
set(gca,'FontSize',TextSize)

subplot(2,1,2)
BESortedFeaturesName=BEData.FeaturesName(BEFeaturesIndex);
stairs(1:FNum+2,[BEData.BestMAE(1:FNum),BEData.MAEALL,BEData.MAEALL],'linewidth',2)
hold on
stairs(1:BESelectNum+1,BEData.BestMAE(1:BESelectNum+1),'r','linewidth',2)
hold off
for i=1:FNum
    text(i+0.5,BEData.BestMAE(i)+0.002-(0.017*(i==1))-(0.009*(i==2)),BESortedFeaturesName{i},'Rotation',90,'FontSize',InFigsTextSize)
end
text(FNum+1.5,BEData.MAEALL+0.005,'All No Fitting','Rotation',90,'FontSize',InFigsTextSize)
text(-2.5,0.245,'B)','FontSize',TextSize,'FontWeight','Bold')
ylim([0.182,0.195])
xlim([0.5,FNum+2.5])
set(gca,'FontSize',TextSize)

% ylabel('Mean Absolute Error');
text(-3.5,0.245,'Mean Absolute Error','FontSize',TextSize+1,'FontWeight','normal','Rotation',90,'HorizontalAlignment','center')
xlabel('Number of Features');
set(gcf,'Position',[50,50,950,600]);
if SavePlots
FileName=[BasePlotFileName,'1']; %#ok<UNRCH>
saveas(gcf,[FileName,'.jpg']);
end
